@model VotingSystem.Models.Survey
@{
    ViewData["Title"] = "问卷结果";
}

<div class="container">
    <h2 class="mb-4">@Model.Title - 结果统计</h2>
    <p class="text-muted">@Model.Description</p>

    <div class="card mb-4">
        <div class="card-body">
            <h5 class="card-title">基本信息</h5>
            <p>总提交数：@Model.TotalResponses</p>
            <p>创建时间：@Model.CreatedAt.ToString("yyyy-MM-dd HH:mm")</p>
            @if (Model.EndTime.HasValue)
            {
                <p>截止时间：@Model.EndTime.Value.ToString("yyyy-MM-dd HH:mm")</p>
            }
        </div>
    </div>

    @foreach (var question in Model.Questions.OrderBy(q => q.Order))
    {
        <div class="card mb-4">
            <div class="card-body">
                <h5 class="card-title">@question.QuestionText</h5>
                <p class="text-muted">问题类型：@question.Type</p>

                @if (question.Type == QuestionType.Text)
                {
                    <div class="list-group">
                        @foreach (var response in question.Responses)
                        {
                            <div class="list-group-item">
                                @response.TextResponse
                            </div>
                        }
                    </div>
                }
                else if (question.Type == QuestionType.Rating)
                {
                    var averageRating = question.Responses
                        .Where(r => r.RatingResponse.HasValue)
                        .Average(r => r.RatingResponse.Value);

                    <div class="progress mb-3" style="height: 20px;">
                        <div class="progress-bar bg-success" role="progressbar"
                             style="width: @(averageRating * 20)%">
                            @averageRating.ToString("F1") 分
                        </div>
                    </div>
                }
                else if (question.Type == QuestionType.SingleChoice || question.Type == QuestionType.MultipleChoice)
                {
                    var totalResponses = question.Responses.Count;
                    foreach (var option in question.Options)
                    {
                        var optionResponses = option.OptionResponses.Count;
                        var percentage = totalResponses > 0 ? (optionResponses * 100.0 / totalResponses) : 0;

                        <div class="mb-3">
                            <div class="d-flex justify-content-between align-items-center mb-1">
                                <span>@option.OptionText</span>
                                <span>@optionResponses 票 (@percentage.ToString("F1")%)</span>
                            </div>
                            <div class="progress" style="height: 20px;">
                                <div class="progress-bar bg-primary" role="progressbar"
                                     style="width: @percentage%">
                                </div>
                            </div>
                        </div>
                    }
                }
            </div>
        </div>
    }

    <div class="form-group">
        <a asp-controller="Surveys" asp-action="Details" asp-route-id="@Model.Id" class="btn btn-secondary">返回问卷</a>
    </div>
</div> 